<template>
  <div class="login">
    <div class="container">
      <div class="content">
        <h4>{{ title }}登录</h4>
        <div class="account-area">
          <el-form
            ref="formRef"
            :rules="loginRules"
            :model="loginForm"
            label-width="auto"
            :hide-required-asterisk="true"
          >
            <el-form-item label="邮箱" prop="account">
              <el-input
                v-model="loginForm.account"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item
              v-if="loginType === 'password'"
              prop="password"
              label="密码"
            >
              <el-input
                v-model="loginForm.password"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item
              v-if="loginType === 'vetifycode'"
              prop="vetifycode"
              label="验证码"
            >
              <el-input
                v-model="loginForm.verifyCode"
                placeholder="请输入六位验证码"
                maxlength="6"
              >
                <template #append>
                  <el-button type="primary" @click="handleSendVerifyCode">
                    获取验证码
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-form>
          <div class="operation-area">
            <el-button class="submit-btn" type="primary" @click="handleLogin">
              登录
            </el-button>
          </div>
        </div>
        <div class="change-login-type">
          <div
            :class="['login-type', loginType === 'vetifycode' && 'active']"
            @click="loginType = 'vetifycode'"
            style="font-size: 15px"
          >
            验证码登陆
          </div>
          <div
            :class="['login-type', loginType === 'password' && 'active']"
            @click="loginType = 'password'"
            style="font-size: 15px"
          >
            账号密码
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
//import { ElMessage } from 'element-plus'
import { computed, reactive, ref, watch } from 'vue'
import { sendEmail } from '@/api/auth/MailApi.ts'
import { loginByCode1 } from '@/api/auth/AuthApi.ts'

const loginRules = {
  account: [
    { required: true, message: '请输入邮箱号码', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不对', trigger: 'blur' },
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}
const formRef = ref<HTMLFormElement>()
const loginForm = reactive({
  account: '',
  password: '',
  verifyCode: '',
})
const loginType = ref<'password' | 'vetifycode' | 'qrcode'>('vetifycode')

const title = computed(() => {
  switch (loginType.value) {
    case 'password':
      return '账号密码'
    case 'vetifycode':
      return '短信验证码'
    default:
      return ''
  }
})

watch(loginType, () => {
  formRef?.value?.resetFields()
})

const handleSendVerifyCode = () => {
  sendEmail(loginForm.account)
}

const handleLogin = () => {
  // console.log(Number(loginForm.verifyCode), loginForm.account, 99);
  // loginByCode(Number(loginForm.verifyCode), loginForm.account)
  //console.log(loginByCode1())
  loginByCode1('123', '123')
}
</script>
<style lang="scss" scoped>
%bg {
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
}

.login {
  min-height: 675px;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/static/imgs/back.jpg');
  @extend %bg;
}

.container {
  width: 350px;
}

.content {
  color: #fff;
  width: 400px;
  //border: 1px solid;
  padding: 20px;
  box-shadow: -15px 15px 15px rgba(22, 6, 47, 0);
  background: rgba(0, 0, 0, 0)
    linear-gradient(230deg, rgba(53, 57, 74, 0) 0%, rgba(0, 0, 0, 0.24) 100%);
}

.account-area {
  margin-top: 50px;
}

:deep(.el-form-item__label) {
  color: #fff;
}

.qr-code-area {
  width: 200px;
  height: 200px;
  margin: 40px auto 20px auto;

  img {
    width: 100%;
    height: 100%;
  }
}

.forget-area {
  float: right;
  font-size: 13px;
  line-height: 16px;
  color: #267ef0;
}

.submit-btn {
  margin: auto 30px 20px 120px;
  width: 30%;
}

.change-login-type {
  display: flex;
  border-top: 1px solid #eee;
  justify-content: space-between;
  padding-top: 10px;

  .login-type {
    font-size: 12px;
    line-height: 16px;
    text-align: center;
    color: #fff;
    width: 25%;
    position: relative;

    &:hover {
      cursor: pointer;
      color: #267ef0;
    }
  }

  .active {
    color: #267ef0;
  }
}
</style>
